package n09.first;

import java.util.Comparator;

public class Teacher {
	private String name; //姓名
	private int age; //年龄
	private int height; //身高
	public enum SortBy { //定义排序的枚举
		Name, NameDec, Age, AgeDec, Height, HeightDec;
	}
	public Teacher(String name,int age, int height) {
		this.name=name;
		this.age=age;
		this.height=height;
	}
	@Override
	public String toString() {
		return name+"\t"+age+"岁\t身高"+height;
	}
	public static Comparator<Teacher> sortBy(SortBy...by) { //返回比较器对象，采用可变参数
		return (o1, o2) -> {
			int index=0, r;
			while(index<by.length) {
				SortBy sb=by[index++]; //获取枚举值
				int key=sb.ordinal()/2;//控制排序的依据 0-name，1-age，2-height
				int d=sb.ordinal()%2==1?-1:1;//升序(1) 降序(-1)
				if(key==0 && (r=o1.name.compareTo(o2.name))!=0)return r*d;
				if(key==1 && (r=o1.age-o2.age)!=0)return r*d;
				if(key==2 && (r=o1.height-o2.height)!=0)return r*d;
			}
			return 0;
		};
	}
}
